package cn.engage.system.service.impl;


import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.system.domain.Menu;
import cn.engage.system.mapper.MenuMapper;
import cn.engage.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-24
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> getTree() {
        List<Menu> list = new ArrayList<>();
        List<Menu> all = menuMapper.findAll();
        Map<Long, Menu> map = new HashMap<>();
        for (Menu menu : all) {
            map.put(menu.getId(),menu);
        }

        for (Menu menu : all) {
            if (menu.getParentId() == null){
                list.add(menu);
            }else {
                Menu menu1 = map.get(menu.getParentId());
                List<Menu> children = menu1.getChildren();
                children.add(menu);
            }
        }
        return list;
    }
}
